package com.boarsoft.boar.app.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.app.AppBiz;
import com.boarsoft.boar.app.AppLibBiz;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.dir.DirBiz;
import com.boarsoft.boar.dir.bean.DirTreeNode;
import com.boarsoft.boar.entity.AppInfo;
import com.boarsoft.boar.entity.DirInfo;
import com.boarsoft.boar.entity.LibInfo;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.util.JsonUtil;

public class AppBaseAction {
	@Autowired
	protected DirBiz dirBiz;
	@Autowired
	protected AppBiz appBiz;
	@Autowired
	protected AppLibBiz appLibBiz;

	protected ReplyInfo<Object> save(String parentId, AppInfo o) {
		if (Util.strIsEmpty(o.getId())) {
			if (Util.strIsEmpty(parentId)) {
				return Constants.REPLY_WARN_INVALID;
			}
			return this.add(parentId, o);
		}
		return this.update(o);
	}

	protected ReplyInfo<Object> add(String parentId, AppInfo o) {
		DirInfo pd = dirBiz.get(parentId);
		if (pd == null) {
			return Constants.REPLY_WARN_INVALID;
		}
		DirInfo d = new DirInfo();
		d.setParentId(parentId);
		d.setType(DirInfo.TYPE_APP);
		d.setCode(o.getCode());
		d.setName(o.getName());
		d.setMemo(o.getMemo());
		// 向上查找应用所属的系统
		String projId = dirBiz.lookup(pd, DirInfo.TYPE_PROJ);
		if (projId == null) {
			return Constants.REPLY_INFO_NOTFOUND;
		}
		o.setProjId(projId);
		ReplyInfo<Object> ro = appBiz.add(d, o);// 此方法将完成相关数据的处理，详见说明
		if (ro.isSuccess()) {
			return new ReplyInfo<>(true, new DirTreeNode(d));
		}
		return ro;
	}

	protected ReplyInfo<Object> update(AppInfo o) {
		DirInfo d = dirBiz.get(o.getId());
		d.setCode(o.getCode());
		d.setName(o.getName());
		d.setMemo(o.getMemo());
		ReplyInfo<Object> ro = dirBiz.update(d);//
		if (!ro.isSuccess()) {
			return new ReplyInfo<Object>(true, String.valueOf(ro.getData()));
		}
		// update方法需判断code是否改变，一旦改变，需修改子节点的path
		appBiz.update(d, o);
		if (ro.isSuccess()) {
			DirTreeNode dtn = new DirTreeNode((DirInfo) ro.getData());
			return new ReplyInfo<Object>(true, dtn);
		}
		return ro;
	}

	/**
	 * 仅当不用树来管理里时使用
	 * 
	 * @return
	 */
	@RequestMapping("/delete.do")
	@Authorized(code = "app.delete")
	public ReplyInfo<Object> delete(String id) {
		appBiz.delete(id);
		return ReplyInfo.SUCCESS;
	}

	/**
	 * 启用应用 根据id更新app_info,dir_info表，设置status为 1 （status-应用状态：0：停用，1：启用 ）
	 * 支持批量启用多个应用，多个应用id用英文逗号分隔
	 * 
	 * @return
	 */
	@RequestMapping("/start.do")
	@Authorized(code = "app.start")
	public ReplyInfo<Object> start(String ids) {
		if (appBiz.start(ids)) {
			return ReplyInfo.SUCCESS;
		}
		return Constants.REPLY_WARN_INVALID;
	}

	/**
	 * 停用应用 根据id更新app_info,dir_info表，设置status为 0 （status-应用状态：0：停用，1：启用 ）
	 * 支持批量停用多个应用，多个应用id用英文逗号分隔
	 * 
	 * @return
	 */
	@RequestMapping("/stop.do")
	@Authorized(code = "app.stop")
	public ReplyInfo<Object> stop(String ids) {
		if (appBiz.stop(ids)) {
			return ReplyInfo.SUCCESS;
		}
		return Constants.REPLY_WARN_INVALID;
	}

	@RequestMapping("/get.do")
	@Authorized(code = "app.get")
	public String get(String id) {
		AppInfo o = appBiz.get(id);
		StringBuilder sb = new StringBuilder();
		sb.append("{ \"success\": true, \"app\": ");
		sb.append(JsonUtil.from(o, "svcs,insts,libs,files,protocols"));
		sb.append(", \"libs\": ");
		List<LibInfo> byLt = appLibBiz.list(id);
		sb.append(JsonUtil.from(byLt, "apps"));
		sb.append(" }");
		return sb.toString();
	}
}